package iscript.gameFramework.IsoLibarys.util 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import iscript.coreFramework.debug.ArthropodDebug;
	import iscript.coreFramework.math.Point2D;
	import iscript.coreFramework.util.StringUtils;
	import iscript.gameFramework.GameLite.graphics.animation.BitmapBatch;
	import iscript.gameFramework.IsoLibarys.display.IsoSprite;
	import iscript.gameFramework.IsoLibarys.display.IsoTile;
	import iscript.gameFramework.IsoLibarys.IsoScene;
	import iscript.gameFramework.IsoLibarys.data.IsoBound;
	import iscript.gameFramework.IsoLibarys.IsoView;
	public class IsoUtil 
	{
		
		public function IsoUtil() 
		{
			
		}
		
		
		
		static public function getIsoTileByPoint(isoview:IsoView):IsoTile {
				 var displayList:Array = isoview.stage.getObjectsUnderPoint(new Point(isoview.stage.mouseX, isoview.stage.mouseY));
				 var isotileList:Vector.<IsoTile> = new Vector.<IsoTile>();
				 for (var i:int = 0; i <displayList.length ; i++) 
				 {
					 if (displayList[i].parent is IsoTile){
						 isotileList.push(displayList[i].parent);
						 }
				 }
				 
				 if (isotileList.length > 0) {
				  var isotile:IsoTile = isotileList[isotileList.length - 1];
				  var row:int = isotile.row;
				  var column:int = isotile.column;              	
				  return isotile; 
				  }
				  return null;
			}
		
		/**
		 * 检测得到的显示对象是不是在IsoSpirte中.
		 * @param	target
		 * @return
		 */
		static  private function  isIsoSprite(target:DisplayObject):DisplayObjectContainer {
			     var p:DisplayObjectContainer;
				 p = target.parent;
				 var t:IsoSprite;
				 while ( p != null) {
					    if (p is IsoSprite) {
							//do something
							t = IsoSprite(p);
							return  t;
							}else {
							p = p.parent;	
						   }
					 }
			     return  t;
			}	
			
		/**
		 * 转换为全局坐标
		 * @param	target
		 * @return
		 */
		static private function toStagePoint(target:DisplayObject):Point {
			   var result:Point = new Point(); 
			   while ( target != null) {
					    result.x += target.x;
						result.y += target.y;
						target = target.parent;
					 }
			     return  result;
			}	
		
		static public function getIsoSpriteByPoint(e:MouseEvent,isoview:IsoView):IsoSprite {
						 var displayList:Array = isoview.stage.getObjectsUnderPoint(new Point(isoview.stage.mouseX, isoview.stage.mouseY));						 
						 var isoSpriteList:Vector.<IsoSprite> = new Vector.<IsoSprite>();
						 for (var i:int = 0; i <displayList.length ; i++) 
						 {
							 var  isoSprite:IsoSprite;
							 if (displayList[i] is Bitmap) {
								   var bd:BitmapData =  displayList[i].bitmapData;
								   var displayPoint:Point = toStagePoint(displayList[i]);
								  if((bd.getPixel32(isoview.stage.mouseX-displayPoint.x,isoview.stage.mouseY-displayPoint.y)>>24& 0xFF)!=0){						   
									 isoSprite =  isIsoSprite(displayList[i]) as IsoSprite;
									}
								 }else{
									isoSprite =  isIsoSprite(displayList[i]) as IsoSprite;
								 }
							 if (isoSprite!=null &&  !isoSprite.lock){isoSpriteList.push(isoSprite);}
						 }	
						 
						 if (isoSpriteList.length > 0) {
						  var isoSprite_:IsoSprite = isoSpriteList[isoSpriteList.length - 1];            	
						  return isoSprite_; 
						  }
						  return null;
					}
			 
		 /**
		  * 根据当前坐标和行数列数得到全部格子数据
		  * @param	v
		  * @param	row
		  * @param	column
		  * @return
		  */	 
		 static public function  extendGrid(v:Point2D,row:int,column:int):IsoBound {
			     var _topGrid:Point2D = new Point2D();
				 var _bottomGrid:Point2D = new Point2D();
				_topGrid.x = v.x - Math.floor(row / 2);
				_topGrid.y = v.y - Math.floor(column / 2);
				_bottomGrid.x = v.x + (row - (Math.floor(row / 2)+1));
				_bottomGrid.y = v.y + (column -  (Math.floor(column / 2) + 1));
				return new IsoBound(_topGrid, _bottomGrid);
			 }	 
 
			 
		
			 
	/*	static  public function getBuilderData(isoscene:IsoScene):String {
			  var floorXMLString:String = "";
			    for (var i:int = 0; i <isoscene.gridSize; i++) 
				{
					for (var j:int = 0; j < isoscene.gridSize; j++) 
					{
						if (Boolean(isoscene.IsBuilder(new Point2D(i, j)))) {
							floorXMLString += 1+","
							}else {
							floorXMLString += 0+","								
							}
					}
				}	
				floorXMLString = StringUtils.removeAt(floorXMLString, floorXMLString.length - 1, floorXMLString.length);				
				return floorXMLString;
			}
			 */
			 
	}

}